package com.ytjj.quartz.task;


import com.alipay.api.response.AlipayMerchantTradecomplainReplySubmitResponse;
import com.ytjj.qmyx.mall.client.MallClient;
import com.ytjj.qmyx.mall.mapper.ComplaintsErrorRecordsMapper;
import com.ytjj.qmyx.mall.model.AliPayComplatintsReplyRequest;
import com.ytjj.qmyx.mall.model.ComplaintsErrorRecords;
import com.ytjj.qmyx.mall.model.WxComplaintsReplyRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
@RefreshScope
public class ComplaintsTask {
    @Resource
    private MallClient mallClient;
    @Resource
    private ComplaintsErrorRecordsMapper complaintsErrorRecordsMapper;

    /**
     * 投诉单自动回复内容
     */
    @Value("${auto.reply.content:您的投诉已由我们商城客服受理，后续将通过电话/微信处理您的问题，您也可以主动拨打我们的官方客服热线0000001706联系我们（上班时间: 9:00-12:00,13:30-18:30)，我们将竭诚为您服务}")
    private String autoReplyContent;

    /**
     * 半个小时执行一次
     * 补偿自动回复错误投诉
     *
     */
//    @Scheduled(cron = "0 0/30 * * * ?")
    public void repairPrepaidOrders(){
        List<ComplaintsErrorRecords> records = complaintsErrorRecordsMapper.queryByStatus(1);
        if (!CollectionUtils.isEmpty(records)) {
            try{
                for (ComplaintsErrorRecords record : records) {
                    if (record.getType().equals(1)) {
                        WxComplaintsReplyRequest wxComplaintsReplyRequest = new WxComplaintsReplyRequest();
                        wxComplaintsReplyRequest.setComplaintedId(record.getComplaintId());
                        if (StringUtils.isEmpty(record.getReplyContent())) {
                            wxComplaintsReplyRequest.setResponseContent(autoReplyContent);
                        } else {
                            wxComplaintsReplyRequest.setResponseContent(record.getReplyContent());
                        }
                        mallClient.wxComplaintsV3Response(wxComplaintsReplyRequest);
                        complaintsErrorRecordsMapper.updateStatus(record.getComplaintId());
                    } else  if (record.getType().equals(2)) {
                        AliPayComplatintsReplyRequest replyRequest = new AliPayComplatintsReplyRequest();
                        replyRequest.setAdminId(1);
                        replyRequest.setComplainEventId(record.getComplaintId());
                        if (StringUtils.isEmpty(record.getReplyContent())) {
                            replyRequest.setReplyContent(autoReplyContent);
                        } else {
                            replyRequest.setReplyContent(record.getReplyContent());
                        }
                        mallClient.complaintsReplySubmit(replyRequest);
                        complaintsErrorRecordsMapper.updateStatus(record.getComplaintId());
                    }

                }
            }catch (Exception e) {
                log.error("补偿自动回复错误投诉");
            }

        }
    }

}
